package com.wpw.dream.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wpw.dream.web.utils.PageInfo;

@Controller
public class PageController {
	
	@RequestMapping("/index.do")
	public String toIndex() {
		return "index";
	}

	@RequestMapping("/listData.do")
	@ResponseBody
	public Object listData(Integer pageNo, Integer pageSize) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("list-data");
		List<Map<String, Object>> list = new ArrayList<>();
		Map<String, Object> map;
		for (int i = 0; i < pageSize; i++) {
			map = new HashMap<>();
			map.put("userName", "wpa" + new Random().nextInt());
			map.put("age", new Random().nextInt());
			list.add(map);
		}
		PageInfo<Map<String, Object>> page = new PageInfo<>(list);
		page.setTotal(10000);
		page.setPageNum(pageNo);
		page.setPageSize(pageSize);
		page.setStartRow(pageSize *(pageNo - 1) + 1);
		page.setEndRow(pageSize*pageNo);
		page.setPages(500);
		page.setNextPage(pageNo + 1);
		page.setPrePage(pageNo -1);
		mv.addObject("page", page);
		return mv;
	}
}
